from django.http import HttpResponse
from django.shortcuts import redirect
from django.views.generic import TemplateView,FormView
from .forms import CommentsForm
from .models import Comments


class CommentView(FormView):
    form_class = CommentsForm
    template_name = 'blog_post.html'

    def form_valid(self, form):
        user = self.request.user
        if not user.pk:
            return HttpResponse('请先返回页面进行登录')
        Comments.objects.create(user=user, **form.cleaned_data)
        return redirect(self.request.META['HTTP_REFERER'])

    def form_invalid(self, form):
        return redirect(self.request.META['HTTP_REFERER'])